package cn.hdu.liang.ch3;

import cn.hdu.liang.ch1.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

public class ConstructorReference {
    public static void main(String[] args) {

        Supplier<Apple> c1 = Apple::new;
        Apple a1 = c1.get();
        System.out.println(a1);

        Function<Integer, Apple> c2 = Apple::new;
        Apple a2 = c2.apply(110);
        System.out.println(a2);

        List<Integer> weights = Arrays.asList(20, 50, 30, 68);
        List<Apple> apples = genAppleList(weights, Apple::new);
        System.out.println(apples);

        BiFunction<String, Integer, Apple> c3 = Apple::new;
        Apple a3 = c3.apply("green", 110);
        System.out.println(a3);

    }

    public static List<Apple> genAppleList(List<Integer> list, Function<Integer, Apple> f) {
        List<Apple> result = new ArrayList<>();
        for(Integer e : list) {
            result.add(f.apply(e));
        }
        return result;
    }
}
